/*     */ package org.jasig.services.persondir.support.jdbc;
/*     */ 
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.regex.Matcher;
/*     */ import java.util.regex.Pattern;
/*     */ import javax.sql.DataSource;
/*     */ import org.apache.commons.lang.StringUtils;
/*     */ import org.apache.commons.lang.Validate;
/*     */ import org.apache.commons.logging.Log;
/*     */ import org.jasig.services.persondir.IPersonAttributeDao;
/*     */ import org.jasig.services.persondir.IPersonAttributes;
/*     */ import org.jasig.services.persondir.support.AbstractQueryPersonAttributeDao;
/*     */ import org.jasig.services.persondir.support.QueryType;
/*     */ import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
/*     */ import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
/*     */ 
/*     */ public abstract class AbstractJdbcPersonAttributeDao<R> extends AbstractQueryPersonAttributeDao<PartialWhereClause>
/*     */ {
/*  63 */   private static final Pattern WHERE_PLACEHOLDER = Pattern.compile("\\{0\\}");
/*     */   private final SimpleJdbcTemplate simpleJdbcTemplate;
/*     */   private final String queryTemplate;
/*  67 */   private QueryType queryType = QueryType.AND;
/*     */ 
/*     */   public AbstractJdbcPersonAttributeDao(DataSource ds, String queryTemplate)
/*     */   {
/*  74 */     Validate.notNull(ds, "DataSource can not be null");
/*  75 */     Validate.notNull(queryTemplate, "queryTemplate can not be null");
/*     */ 
/*  77 */     this.simpleJdbcTemplate = new SimpleJdbcTemplate(ds);
/*  78 */     this.queryTemplate = queryTemplate;
/*     */   }
/*     */ 
/*     */   public String getQueryTemplate()
/*     */   {
/*  85 */     return this.queryTemplate;
/*     */   }
/*     */ 
/*     */   public QueryType getQueryType()
/*     */   {
/*  92 */     return this.queryType;
/*     */   }
/*     */ 
/*     */   public void setQueryType(QueryType queryType)
/*     */   {
/* 100 */     this.queryType = queryType;
/*     */   }
/*     */ 
/*     */   protected abstract List<IPersonAttributes> parseAttributeMapFromResults(List<R> paramList, String paramString);
/*     */ 
/*     */   protected abstract ParameterizedRowMapper<R> getRowMapper();
/*     */ 
/*     */   protected PartialWhereClause appendAttributeToQuery(PartialWhereClause queryBuilder, String dataAttribute, List<Object> queryValues)
/*     */   {
/* 123 */     for (Iterator i$ = queryValues.iterator(); i$.hasNext(); ) { Object queryValue = i$.next();
/* 124 */       String queryString = queryValue != null ? queryValue.toString() : null;
/* 125 */       if (StringUtils.isNotBlank(queryString)) {
/* 126 */         if (queryBuilder == null) {
/* 127 */           queryBuilder = new PartialWhereClause();
/*     */         }
/* 129 */         else if (queryBuilder.sql.length() > 0) {
/* 130 */           queryBuilder.sql.append(" ").append(this.queryType.toString()).append(" ");
/*     */         }
/*     */ 
/* 134 */         Matcher queryValueMatcher = IPersonAttributeDao.WILDCARD_PATTERN.matcher(queryString);
/* 135 */         String formattedQueryValue = queryValueMatcher.replaceAll("%");
/*     */ 
/* 137 */         queryBuilder.arguments.add(formattedQueryValue);
/* 138 */         if (dataAttribute != null) {
/* 139 */           queryBuilder.sql.append(dataAttribute);
/* 140 */           if (formattedQueryValue.equals(queryString)) {
/* 141 */             queryBuilder.sql.append(" = ");
/*     */           }
/*     */           else {
/* 144 */             queryBuilder.sql.append(" LIKE ");
/*     */           }
/*     */         }
/* 147 */         queryBuilder.sql.append("?");
/*     */       }
/*     */     }
/*     */ 
/* 151 */     return queryBuilder;
/*     */   }
/*     */ 
/*     */   protected List<IPersonAttributes> getPeopleForQuery(PartialWhereClause queryBuilder, String queryUserName)
/*     */   {
/* 161 */     ParameterizedRowMapper rowMapper = getRowMapper();
/*     */     List results;
/* 164 */     if (queryBuilder != null)
/*     */     {
/* 166 */       StringBuilder partialSqlWhere = queryBuilder.sql;
/* 167 */       Matcher queryMatcher = WHERE_PLACEHOLDER.matcher(this.queryTemplate);
/* 168 */       String querySQL = queryMatcher.replaceAll(partialSqlWhere.toString());
/*     */ 
/* 170 */        results = this.simpleJdbcTemplate.query(querySQL, rowMapper, queryBuilder.arguments.toArray());
/*     */ 
/* 172 */       if (this.logger.isDebugEnabled())
/* 173 */         this.logger.debug("Executed '" + this.queryTemplate + "' with arguments " + queryBuilder.arguments + " and got results " + results);
/*     */     }
/*     */     else
/*     */     {
/* 177 */       results = this.simpleJdbcTemplate.query(this.queryTemplate, rowMapper, new Object[0]);
/*     */ 
/* 179 */       if (this.logger.isDebugEnabled()) {
/* 180 */         this.logger.debug("Executed '" + this.queryTemplate + "' and got results " + results);
/*     */       }
/*     */     }
/*     */ 
/* 184 */     return parseAttributeMapFromResults(results, queryUserName);
/*     */   }
/*     */ }

/* Location:           C:\Users\Administrator\Desktop\person-directory-impl-1.5.1.jar
 * Qualified Name:     org.jasig.services.persondir.support.jdbc.AbstractJdbcPersonAttributeDao
 * JD-Core Version:    0.6.2
 */